package com.huaxiafinance.management.controller;

import com.huaxiafinance.management.dto.request.PackageDto;
import com.huaxiafinance.management.service.PartnersDetailsService;
import com.huaxiafinance.management.util.MessageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


@Controller("partnersDetailsController")
@RequestMapping("/partnersDetails")
public class PartnersDetailsController {

  protected Logger logger = Logger.getLogger(PartnersDetailsController.class);
  @Autowired
  private PartnersDetailsService partnersDetailsService;

  /**
   * 查询合伙人套餐状态列表
   */
  @RequestMapping(value = "/queryPackageType", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryPackageType() {
    MessageResult messageResult = new MessageResult();
    try {
      messageResult = partnersDetailsService.queryPackageType();
    } catch (Exception e) {
      messageResult.setFailureResult();
      logger.info("查询合伙人套餐状态列表异常原因：" + e);
    }
    logger.info("查询合伙人套餐状态列表返回参数：" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }


  /**
   * 查询用户个人信息详情
   */
  @RequestMapping(value = "/queryPartner", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryPartner(HttpServletRequest request) throws Exception {
    logger.info("查询用户个人信息参数：" + request.toString());
    String userUuid = request.getParameter("userUuid");
    MessageResult messageResult = new MessageResult();
    try {
      messageResult = partnersDetailsService.queryPartner(userUuid);
    } catch (Exception e) {
      messageResult.setFailureResult();
      logger.info("查询用户个人信息异常原因：" + e);
    }
    logger.info("查询用户个人信息返回参数：" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 获取套餐列表
   */
  @RequestMapping(value = "/queryPackageDetails", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryPackageDetails(@Valid @RequestBody PackageDto dto, BindingResult bindingResult)
      throws Exception {
    logger.info("请求套餐详情的参数:" + dto.toString());
    MessageResult messageResult = new MessageResult();
    try {
      if (bindingResult.hasErrors()) {
        messageResult.setFailureResult(bindingResult);
        return messageResult.getAsJSON();
      }
      messageResult = partnersDetailsService.queryMembershipMeal(dto);
    } catch (Exception e) {
      logger.info("请求套餐详情的异常原因:" + e);
      messageResult.setFailureResult();
    }
    logger.info("请求套餐详情的返回参数:" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }


}
